package com.ldf.jrtsp.enums;

import com.ldf.jrtsp.callback.BareStreamCallBack;
import com.ldf.jrtsp.exception.JRtspException;
import com.ldf.jrtsp.parser.*;

import java.lang.reflect.InvocationTargetException;

/**
 * rtp负载类型
 *
 * @author lidaofu
 * @since 2024/6/19
 **/
public enum RtpPayloadType {

    PCMU(MediaType.AUDIO),
    PCMA(MediaType.AUDIO, RtpLoadPCMADataParser.class),
    G722(MediaType.AUDIO, RtpLoadPCMUDataParser.class),
    H264(MediaType.VIDEO, RtpLoadH264DataParser.class),
    H265(MediaType.VIDEO, RtpLoadH265DataParser.class),
    OPUS(MediaType.VIDEO),
    UNKNOWN(MediaType.VIDEO);

    private final MediaType mediaType;

    private final Class<? extends IRtpLoadCodecParser> iRtpOverCodecParserClass;

    RtpPayloadType(MediaType mediaType, Class<? extends IRtpLoadCodecParser> ipOverCodecParser) {
        this.mediaType = mediaType;
        this.iRtpOverCodecParserClass = ipOverCodecParser;
    }

    RtpPayloadType(MediaType mediaType) {
        this.mediaType = mediaType;
        this.iRtpOverCodecParserClass = null;
    }

    public static RtpPayloadType getRespPayloadType(String codec) {
        RtpPayloadType[] values = values();
        for (RtpPayloadType value : values) {
            if (value.name().equalsIgnoreCase(codec)) {
                return value;
            }
        }
        return UNKNOWN;
    }

    /**
     * 获取RtpOverCodecParser
     *
     * @param bareStreamCallBack
     * @return
     */
    public IRtpLoadCodecParser getRtpOverCodecParser(BareStreamCallBack bareStreamCallBack) {
        if (iRtpOverCodecParserClass != null) {
            try {
                return iRtpOverCodecParserClass.getConstructor(BareStreamCallBack.class).newInstance(bareStreamCallBack);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                     NoSuchMethodException e) {
                throw new JRtspException(e);
            }
        }
        return null;
    }

    public MediaType getMediaType() {
        return mediaType;
    }

}
